package com.ycsj.goldmedalnewconcept.parent.activity;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.base.OnloadingMoreListener;
import com.ycsj.common.manager.BqaManager;
import com.ycsj.common.manager.SrfManager;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.adapter.EventAdapter;
import com.ycsj.goldmedalnewconcept.common.bean.EventInfo;
import com.ycsj.goldmedalnewconcept.common.util.EventUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 活动
 * Created by Administrator on 2018/6/25 0025.
 */

public class ParentEventActivity extends BaseActivity implements SrfManager.OnPullRefreshListener, OnloadingMoreListener, View.OnClickListener {

    //下拉刷新
    @BindView(R.id.srf)
    SmartRefreshLayout srf;

    @BindView(R.id.tv_title)
    TextView tvTitle;

    //列表控件
    @BindView(R.id.rv)
    RecyclerView rv;

    //加载框
    @BindView(R.id.rl_loading)
    RelativeLayout rlLoading;

    //内容视图
    @BindView(R.id.rl_content)
    RelativeLayout rlContent;

    //数据集合
    private List<EventInfo.ListBean> oldDatas;

    //空视图
    private View emptyView;

    //数据适配器
    private EventAdapter adapter;

    //页面更新的标识
    public static final int UPDATE = 0x532;


    @Override
    protected int getContentLayout() {
        return R.layout.activity_event;
    }

    @Override
    protected void init() {

        //状态栏
        initStateBar();

        //初始化视图
        initTitle();

        //初始化下拉刷新
        initSrf();

        //初始化列表
        initRv();

        //获取数据
        http(BqaManager.NORML);

    }

    /**
     * 初始化视图
     */
    private void initTitle() {
        tvTitle.setText("活 动");
    }

    /**
     * 设置监听
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, this);
    }


    /**
     * 设置列表数据
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view, null);
        TextView tvMessage = emptyView.findViewById(R.id.tv_content);
        tvMessage.setText("暂时没有新的活动内容");
        adapter = new EventAdapter(this, R.layout.parent_rv_event, oldDatas);

        //条目点击事件
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //整体条目的点击事件
                // 跳转到活动详情界面
                Intent intent = new Intent(ParentEventActivity.this, ParentsEventDetailsActivity.class);
                intent.putExtra("data", oldDatas.get(position));
                startActivity(intent);
            }
        });
    }

    /**
     * 请求数据
     *
     * @param method
     */
    private void http(int method) {

        EventUtils.httpEvent(this, method, "","", new EventUtils.OnRequestListener() {

            @Override
            public void onSuccess(int method, List<EventInfo.ListBean> newDatas) {
                if (method == BqaManager.NORML) {
                    if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                        rlLoading.setVisibility(View.GONE);
                    }
                    if (rlContent != null && rlContent.getVisibility() != View.VISIBLE) {
                        rlContent.setVisibility(View.VISIBLE);
                    }
                    oldDatas.clear();
                    oldDatas.addAll(newDatas);
                    BqaManager.setRv(emptyView, ParentEventActivity.this, adapter, rv, ParentEventActivity.this);
                } else {
                    //更新数据
                    BqaManager.updateRvBySmf(method, srf, rv, oldDatas, newDatas, adapter);
                }
            }

            @Override
            public void onStateError(int method) {

                if (method == BqaManager.NORML) {
                    if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                        rlLoading.setVisibility(View.GONE);
                    }
                } else if (method == BqaManager.PULLREFRESH) {
                    //下拉刷新的时候关闭下拉刷新
                    if (srf != null) {
                        srf.finishRefresh(false);
                    }
                } else if (method == BqaManager.LOADINDGMORE) {
                    //加载更多的时候关闭加载更多
                    adapter.loadMoreComplete();
                }
            }

            @Override
            public void onError(int method) {
                BqaManager.setErrorNet(rlLoading, srf, method, adapter);
            }



        });
    }

    /**
     * 下拉刷新
     *
     * @param refreshlayout
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        http(BqaManager.PULLREFRESH);
    }


    /**
     * 加载更多
     */
    @Override
    public void onLoadMore() {
        http(BqaManager.LOADINDGMORE);
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.iv_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                //返回
                finish();
                break;

        }
    }



    @Override
    public void busEvent(BusInfo busInfo) {
        if (null != busInfo && UPDATE == busInfo.type) {
            http(BqaManager.PULLREFRESH);
        }
    }
}
